﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QUAN_LY_NGAN_HANG_CAU_HOI_TRAC_NGHIEM
{
    public partial class FrmTaoMoiCauHoi : Form
    {
        public FrmTaoMoiCauHoi(TaiKhoanDAL tk)
        {
            InitializeComponent();
            taikhoan = tk;
        }

        private TaiKhoanDAL taikhoan;
        private CauHoiDAL cauhoi;
        private List<DapAnDAL> lst = new List<DapAnDAL>();
        private DapAnDAL dapan;
        private TaoCauHoiPresenter pre;

        private void linkThoat_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            this.Close();
        }

        private void linkThemCauHoi_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (KiemTraThongTin() == true)
            {
                cauhoi = new CauHoiDAL();
                cauhoi.GiaoVien = taikhoan.GiaoVien;
                if (cboLinhVuc.SelectedIndex >= 0)
                    cauhoi.LinhVuc = ((LinhVucBUS) cboLinhVuc.SelectedItem).MaSoLinhVuc;
                if (cboMucDo.SelectedIndex >= 0)
                    cauhoi.MucDo = ((MucDoBUS) cboMucDo.SelectedItem).MaSoMucDo;
                cauhoi.NoiDung = txtNoiDung.Text;
                cauhoi.TanSuatDung = 0;

                DataGridViewCheckBoxCell ocell;
                for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
                {
                    dapan = new DapAnDAL();
                    dapan.NoiDungDapAn = (string) dataGridView1[0, i].Value.ToString();
                    dapan.STTDapAn = i + 1;

                    ocell = dataGridView1.Rows[i].Cells[1] as DataGridViewCheckBoxCell;
                    bool check = (null != ocell && null != ocell.Value && true == (bool) ocell.Value);
                    if (check == true)
                    {
                        dapan.DapAnDung = true;
                    }
                    else dapan.DapAnDung = false;
                    lst.Add(dapan);
                }
                try
                {
                    pre.InsertCauHoiVaDapAn(cauhoi, lst);
                    MessageBox.Show("Them Cau Hoi Thanh Cong", "Thong Bao", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txtNoiDung.Text = "";
                    dataGridView1.Rows.Clear();
                    cboLinhVuc.Text = "Chon";
                    cboMucDo.Text = "Chon";
                }
                catch(Exception loi)
                {
                    MessageBox.Show(loi.Message);
                }
            }
            else
            {
                MessageBox.Show("Thong tin nhap vao khong day du", "Thong Bao", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void FrmTaoMoiCauHoi_Load(object sender, EventArgs e)
        {
            pre = new TaoCauHoiPresenter();
            cboLinhVuc.DataSource = pre.ListLinhVucTheoGiaoVien(taikhoan.GiaoVien);
            cboLinhVuc.ValueMember = "MaSoLinhVuc";
            cboLinhVuc.DisplayMember = "ChuThich";

            cboMucDo.DataSource = pre.ListMucDo();
            cboMucDo.ValueMember = "MaSoMucDo";
            cboMucDo.DisplayMember = "ChuThich";

            cboLinhVuc.Text = "Chon";
            cboMucDo.Text = "Chon";
            this.Left = 50;
            this.Top = 0;

            this.helpProvider1.SetHelpNavigator(this, HelpNavigator.Topic);
            this.helpProvider1.SetHelpKeyword(this, "ToMiCauHi.html");
        }

        private bool KiemTraThongTin()
        {
            bool kq = false;
            if (cboLinhVuc.SelectedText == "Chon")
                return kq;
            if (cboMucDo.SelectedText == "Chon")
                return kq;
            if (txtNoiDung.Text == "")
                return kq;
            if (dataGridView1.Rows.Count < 1)
                return kq;
            else
            {
                DataGridViewCheckBoxCell ocell;
                for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
                {
                    ocell = dataGridView1.Rows[i].Cells[1] as DataGridViewCheckBoxCell;
                    bool check = (null != ocell && null != ocell.Value && true == (bool)ocell.Value);
                    if (check == true)
                    {
                        kq = true;
                        break;
                    }
                }
            }
            return kq;
        }

        private void linkBoCauHoi_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            txtNoiDung.Text = "";
            dataGridView1.Rows.Clear();
            cboLinhVuc.Text = "Chon";
            cboMucDo.Text = "Chon";
        }
    }
}
